package com.greenold.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;
import java.util.UUID;

public class OSSUtils {
    // 你在阿里云 OSS 控制台创建的参数
    private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
    private static final String accessKeyId = "LTAI5tL84E5ayo1x7Fsw576D";
    private static final String accessKeySecret = "iP43qfJQv7lKu93pBVVSrFhM9Og53J";
    private static final String bucketName = "bucket-yueyang";

    public static String upload(InputStream inputStream, String originalFilename, String folder) {
        // ✅ 在这里指定你要上传的文件夹路径
        folder = "uploads/images/";
        // 生成唯一文件名，避免重复
        String fileName = folder + UUID.randomUUID() + "-" + originalFilename;
        // 上传到 OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);
        ossClient.shutdown();
        // 返回访问 URL
        return "https://" + bucketName + ".oss-cn-shanghai.aliyuncs.com/" + fileName;
    }
}